<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Banner;
use App\Models\Category;
use App\Models\Config;
use App\Models\News;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;


class H5Controller extends BaseController
{


    public function homePage(Request $request) {
        $type = $request->input('type', 1);
        $banner_list = Banner::orderBy('sort', 'desc')->where('from', $type)->orderBy("id", "desc")
            ->get();
        $cat_list = Category::where('parent_id', 0)->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->get();

        $field = ['id', 'name', 'adv_map', 'amount', 'company_title', 'logo'];
        $rec_project_list = Project::where('is_rec', 1)->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")
            ->get();

        $news_type_list = Config::where('key', 'news_type')->limit(4)->get();

        $first_news_type_id = $news_type_list->first()->id;
        $field = ['id', 'name', 'content', 'created_at', 'image_url', 'short_content'];
        $news_list = News::where('news_type', $first_news_type_id)->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")->paginate(per_page());
        $news_list = $this->paginate2($news_list);

        $site = Config::where('key', 'site')->first();
        $site = json_decode($site->value, true);
        return $this->success(compact('banner_list', 'cat_list', 'rec_project_list', 'news_type_list', 'news_list', 'site'));
    }

    public function newsList(Request $request) {
        $news_type = $request->input('news_type');
        $field = ['id', 'name', 'content', 'created_at', 'image_url', 'short_content'];
        $name = $request->input('name');
        $list = News::when($news_type, function ($q) use($news_type){
            $q->where('news_type', $news_type);
        })->when($name, function ($q) use ($name) {
            $q->where('name', 'like', '%' . $name . '%');
        })->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")->paginate(per_page());
        $news_type_list = Config::where('key', 'news_type')->get();

        $new_list = $this->paginate2($list);
        return $this->success(compact('news_type_list', 'new_list'));
    }

    public function projectList(Request $request) {
        $input = $request->input();
        $search = ['category_id' => 0, 'parent_category_id' => 0];
        $amount = $request->input('amount');
        $field = ['id', 'name', 'adv_map', 'amount', 'company_title', 'category_id', 'parent_category_id', 'logo'];
        $is_rank = $request->input('is_rank');
        $name = $request->input('name');
        $list = Project::apply($search, $input)
            ->withCertain('category', ['title'])
            ->withCertain('parent_category', ['title'])
            ->when($amount, function ($q) use($amount){
                $q->where('amount', $amount);
            })
            ->when($name, function ($q) use ($name) {
                $q->where('name', 'like', '%' . $name . '%');
            })
            ->orderBy("sort", "desc")->when($is_rank, function ($q) {
                $q->where('is_rank', 1);
            })->orderBy("id", "desc")
            ->select($field)
            ->paginate(per_page());
        foreach ($list as $item) {
            $item->append(['amount_show']);
        }
        $projectList = $this->paginate2($list);

        $catList = Category::with('child')->where('parent_id', 0)->orderBy("id", "desc")
            ->get();

        $amountList = Config::where('key', 'amount')->get();

        return $this->success(compact('projectList', 'catList', 'amountList'));
    }

    public function projectInfo(Request $request)
    {
        $model = new Project();
        $table = $model->getTable();
        $this->validate($request, [
            'id' => 'required|exists:' . $table . ",id"
        ]);
        $id = \request("id");
        $info = $model->withCertain('category', ['title'])
            ->withCertain('parent_category', ['title'])->find($id);
        if (!$info)
            return $this->failed('项目不存在');
        $info->append(['amount_show', 'tag_ids_show']);
        $cache_key = 'project_' . $id;
        $pv = Redis::incr($cache_key, 1);
        if ($pv % 10 == 0) {
            $info->pv = $pv;
            $info->save();
        }

        //相关的推荐项目
        $field = ['id', 'name', 'adv_map', 'amount', 'logo'];
        $relate_project_list = Project::where('parent_category_id', $info->parent_category_id)
            ->where('id', "!=", $id)
            ->where('is_rec', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($field)
            ->paginate(per_page(2));
        $relate_project_list =  $this->paginate2($relate_project_list);


        $field = ['id', 'name', 'content', 'created_at', 'image_url'];
        $relate_news_list = News::where('parent_category_id', $info->parent_category_id)->select($field)->orderBy("sort", "desc")->orderBy("id", "desc")->paginate(per_page(10));
        $relate_news_list =  $this->paginate2($relate_news_list);

        return $this->success(compact('info', 'relate_news_list', 'relate_project_list'));
    }

    public function newsInfo(Request $request) {
        $id = \request("id");
        $info =  News::with(['project'=>function($q) {
            $q->select(['id', 'name', 'category_id', 'parent_category_id', 'company_title', 'amount']);
        }, 'project.category' => function($q) {
            $q->select(['id', 'title']);
        }, 'project.parent_category' => function($q) {
            $q->select(['id', 'title']);
        }])->find($id);

        $cache_key = 'news_' . $id;
        $pv = Redis::incr($cache_key, 1);
        if ($pv % 10 == 0) {
            $info->pv = $pv;
            $info->save();
        }

        $category_id = $info->project->parent_category_id;
        //相关的推荐项目
        $field = ['id', 'name', 'adv_map', 'amount', 'logo'];
        $relate_project_list = Project::where('parent_category_id', $category_id)
            ->where('id', "!=", $id)
            ->where('is_rec', 1)
            ->orderBy("sort", "desc")->orderBy("id", "desc")
            ->select($field)
            ->paginate(per_page(2));
        $relate_project_list =  $this->paginate2($relate_project_list);

        $field = ['id', 'name', 'content', 'created_at', 'image_url', 'short_content'];
        $relate_news_list = News::where('project_id', $info->project_id)->select($field)
            ->orderBy("sort", "desc")->where('id', '!=', $id)->orderBy("id", "desc")->paginate(per_page(10));
        $relate_news_list =  $this->paginate2($relate_news_list);

        return $this->success(compact('info', 'relate_project_list', 'relate_news_list'));
    }


}

